package com.myBlog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.myBlog.constants.SystemConstants;
import com.myBlog.domain.ResponseResult;
import com.myBlog.domain.dto.CategoryDTO;
import com.myBlog.domain.entity.Article;
import com.myBlog.domain.entity.Category;
import com.myBlog.domain.vo.CategoryVo;
import com.myBlog.domain.vo.PageVo;
import com.myBlog.enums.AppHttpCodeEnum;
import com.myBlog.exception.SystemException;
import com.myBlog.mapper.CategoryMapper;
import com.myBlog.service.ArticleService;
import com.myBlog.service.CategoryService;
import com.myBlog.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2023-03-13 16:57:04
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ArticleService articleService;

    /**
     * 获取文章
     *
     * @return
     */
    @Override
    public ResponseResult getCategoryList() {
        //单表查询，且状态为发布的文章，先查Article得到CategoryList
        LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper<>();
        articleWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_PUBLISH);
        List<Article> articleList = articleService.list(articleWrapper);
        //获取文章的分类id，并且去重
        Set<Long> categoryIds = articleList.stream()
                .map(Article::getCategoryId)
                .collect(Collectors.toSet());

        //查询分类表
        List<Category> categoryList = listByIds(categoryIds);
        categoryList = categoryList.stream()
                .filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        //封装为Vo
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categoryList, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    /**
     * 查询所有分类接口
     *
     * @return
     */
    @Override
    public List<CategoryVo> listAllCategory() {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus, SystemConstants.STATUS_NORMAL);
        List<Category> categoryList = list(wrapper);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categoryList, CategoryVo.class);
        return categoryVos;
    }

    /**
     * 分页条件查询
     *
     * @param pageNum
     * @param pageSize
     * @param categoryDTO
     * @return
     */
    @Override
    public ResponseResult<PageVo> list(Integer pageNum, Integer pageSize, CategoryDTO categoryDTO) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();

        //不为空添加一个模糊查询
        wrapper.like(StringUtils.hasText(categoryDTO.getName()), Category::getName, categoryDTO.getName());

        //不为空添加一个查询条件
        wrapper.in(StringUtils.hasText(categoryDTO.getStatus()), Category::getStatus, categoryDTO.getStatus());
        Page<Category> categoryPage = new Page<>(pageNum, pageSize);
        page(categoryPage, wrapper);
        PageVo pageVo = new PageVo(categoryPage.getRecords(), categoryPage.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    /**
     * 根据分类id查询分类信息
     *
     * @param categoryId
     * @return
     */
    @Override
    public ResponseResult getCategoryInfo(Long categoryId) {
        Category category = getById(categoryId);
        CategoryDTO categoryDTO = BeanCopyUtils.copyBean(category, CategoryDTO.class);
        return ResponseResult.okResult(categoryDTO);
    }

    /**
     * 根据id删除类别
     *
     * @param ids
     * @return
     */
    @Override
    public ResponseResult deleteByIds(List<Long> ids) {
        List<Article> articles = articleService.listByIds(ids);
        if (articles.size() > 0) {
            throw new SystemException(AppHttpCodeEnum.CATEGORY_HAS_LINK);
        } else {
            List<Category> categoryList = listByIds(ids);
            categoryList = categoryList.stream()
                    .peek(category -> category.setDelFlag(1))
                    .collect(Collectors.toList());
            removeBatchByIds(categoryList);

        }
        return ResponseResult.okResult();
    }

    /**
     * 添加分类
     *
     * @param categoryDTO
     * @return
     */
    @Override
    public ResponseResult addCategory(CategoryDTO categoryDTO) {
        if (!StringUtils.hasText(categoryDTO.getName())) {
            throw new SystemException(AppHttpCodeEnum.CATEGORYNAME_NOT_NULL);
        }
        Category category = BeanCopyUtils.copyBean(categoryDTO, Category.class);
        this.save(category);

        return ResponseResult.okResult();
    }

    /**
     * 修改分类
     *
     * @param categoryDTO
     * @return
     */
    @Override
    public ResponseResult updateCategory(CategoryDTO categoryDTO) {
        if (null == categoryDTO.getId() || !StringUtils.hasText(categoryDTO.getName())) {
            throw new SystemException(AppHttpCodeEnum.TAGNAME_NOT_NULL);
        } else {
            Category category = BeanCopyUtils.copyBean(categoryDTO, Category.class);
            updateById(category);
        }
        return ResponseResult.okResult();
    }
}

